package com.yingjia.yingjiauniversity.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * TimeUtils
 *
 * @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2013-8-24
 */
public class TimeUtil {

    /**
     * data format
     */
    public static final SimpleDateFormat DEFAULT_DATE_FORMAT() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    }

    public static final SimpleDateFormat DATE_FORMAT_DATE() {
        return new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
    }

    public static final SimpleDateFormat DATE_FORMAT2() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
    }

    public static final SimpleDateFormat DATE_UNDERLINE_FORMAT() {
        return new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss", Locale.getDefault());
    }

    public static final SimpleDateFormat DATE_UNDERLINE_FORMAT2() {
        return new SimpleDateFormat("yyyy_MM_dd_HH_mm", Locale.getDefault());
    }

    /**
     * time format
     */
    public static final SimpleDateFormat DEFAULT_TIME_FORMAT() {
        return new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
    }

    public static final SimpleDateFormat DETAIL_TIME_FORMAT() {
        return new SimpleDateFormat("HH_mm_ss.SSS", Locale.getDefault());
    }

    private TimeUtil() {
        throw new AssertionError();
    }

    /**
     * long time to string
     *
     * @param timeInMillis
     * @param dateFormat
     * @return
     */
    public static String getTime(long timeInMillis, SimpleDateFormat dateFormat) {
        return dateFormat.format(new Date(timeInMillis));
    }

    /**
     * long time to string, format is {@link #DEFAULT_DATE_FORMAT()}
     *
     * @param timeInMillis
     * @return
     */
    public static String getTime(long timeInMillis) {
        return getTime(timeInMillis, DEFAULT_DATE_FORMAT());
    }

    /**
     * get current time in milliseconds
     *
     * @return
     */
    public static long getCurrentTimeInLong() {
        return System.currentTimeMillis();
    }

    /**
     * get current time in milliseconds, format is {@link #DEFAULT_DATE_FORMAT()}
     *
     * @return
     */
    public static String getCurrentTimeInString() {
        return getTime(getCurrentTimeInLong());
    }

    /**
     * get current time in milliseconds
     *
     * @return
     */
    public static String getCurrentTimeInString(SimpleDateFormat dateFormat) {
        return getTime(getCurrentTimeInLong(), dateFormat);
    }

    /**
     * Gets time in long.
     *
     * @param timeStr the time str
     * @return the time in long
     */
    public static long getTimeInLong(String timeStr) {
        return getTimeInLong(timeStr, DEFAULT_DATE_FORMAT());
    }

    /**
     * Gets time in long.
     *
     * @param timeStr    the time str
     * @param dateFormat the date format
     * @return the time in long
     */
    public static long getTimeInLong(String timeStr, SimpleDateFormat dateFormat) {
        try {
            Date date = dateFormat.parse(timeStr);
            return date.getTime();
        } catch (ParseException e) {
        }
        return 0;
    }

    /**
     * Sets time part.
     *
     * @param srcTimeInMillis  原始时间毫秒
     * @param timePartInMillis 需要替换时间部分(HHmmssSSS)的时间毫秒
     * @return the time part
     */
    public static long setTimePart(long srcTimeInMillis, long timePartInMillis) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(srcTimeInMillis);
        Calendar partCalendar = Calendar.getInstance();
        partCalendar.setTimeInMillis(timePartInMillis);
        calendar.set(Calendar.HOUR_OF_DAY, partCalendar.get(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, partCalendar.get(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, partCalendar.get(Calendar.SECOND));
        calendar.set(Calendar.MILLISECOND, partCalendar.get(Calendar.MILLISECOND));
        return calendar.getTimeInMillis();
    }
}
